/*
 * @Author: 梁 卓
 * @Version: V1.0.0
 * @Date: 2024-09-29 20:26:27
 * @LastEditors: 梁 卓
 * @LastEditTime: 2024-10-08 20:28:26
 * @FilePath: request.js
 * Copyright 2024 Marvin, All Rights Reserved. 
 * 2024-09-29 20:26:27
 * @Description: 
 */
import axios from 'axios'
import { ElMessage } from 'element-plus'

// 创建axios实例
const service = axios.create({
    // axios中请求配置有baseURL选项，表示请求URL公共部分
    baseURL: 'http://127.0.0.1:8080/',
    // 超时
    timeout: 10000
  })

//请求拦截
service.interceptors.request.use((config) => {
    const token = localStorage.getItem("login-token");
    if (token){
        config.headers['Authorization'] = token
    }
    return config;
},(error)=>{
    return Promise.reject(error);
})

// 响应拦截
service.interceptors.response.use((response) => {
    console.log('**********请求地址***********' , response.config.url)
    console.log('**********请求响应***********' , response.data)
    let responseData = response.data;
    if (responseData.code == 200){
        return response.data;
    } else if (responseData.code == 500){
        ElMessage.error("请重新登录！！！")
        window.location.href = "/login"
    } else {
        ElMessage.error(responseData.msg)
    }
},(error)=>{
    return Promise.reject(error)
})

export default service
